#include <kdma.h>
#include <csp_shared_types.h>
#include <spu_mfcio.h>
#include <stdio.h>

void write_mbox_blocking(uint32_t msg)
{
	spu_write_out_intr_mbox(msg);
}


void little_get(ea_addr_t from, ls_addr_t to, ls_size_t size, int slot){
	mfc_get((void*) to, from, size, slot, 0, 0); 
#ifdef BLOCK_ALL_DMA
	mfc_write_tag_mask(1<<slot);
	mfc_read_tag_status_all();
#endif
}

void little_put(ls_addr_t from, ea_addr_t to, ls_size_t size, int slot){
	mfc_put((void*) from, to, size, slot, 0, 0); 
#ifdef BLOCK_ALL_DMA
	mfc_write_tag_mask(1<<slot);
	mfc_read_tag_status_all();
#endif
}

void little_put_barrier(ls_addr_t from, ea_addr_t to, ls_size_t size, int slot){
	mfc_putb((void*) from, to, size, slot, 0, 0); 
#ifdef BLOCK_ALL_DMA
	mfc_write_tag_mask(1<<slot);
	mfc_read_tag_status_all();
#endif
}

void little_put_blocking(ls_addr_t from, ea_addr_t to, ls_size_t size, int slot){
	mfc_put((void*) from, to, size, slot, 0, 0); 
	mfc_write_tag_mask(1<<slot);
	mfc_read_tag_status_all();
}

void little_get_blocking(ea_addr_t from, ls_addr_t to, ls_size_t size, int slot){
	mfc_get((void*) to, from, size, slot, 0, 0); 
	mfc_write_tag_mask(1<<slot);
	mfc_read_tag_status_all();
}

void large_get(ea_addr_t from, ls_addr_t to, ls_size_t size, int slot){
	unsigned int sz;
	while(size > 0){
		sz = (size < _16KB) ? size : _16KB;
		size -= sz;
		mfc_get((void*) to, from, sz, slot, 0, 0); 
#ifdef BLOCK_ALL_DMA
		mfc_write_tag_mask(1<<slot);
		mfc_read_tag_status_all();
#endif
		to += sz;
		from += sz;
	}
}

void large_put(ls_addr_t from, ea_addr_t to, ls_size_t size, int slot){
	unsigned int sz;
	while(size > 0){
		sz = (size < _16KB) ? size : _16KB;
		size -= sz;
		mfc_put((void*) from, to, sz, slot, 0, 0); 
#ifdef BLOCK_ALL_DMA
		mfc_write_tag_mask(1<<slot);
		mfc_read_tag_status_all();
#endif
		to += sz;
		from += sz;
	}
}

void large_put_blocking(ls_addr_t from, ea_addr_t to, ls_size_t size, int slot){
	large_put(from, to, size, slot);
	mfc_write_tag_mask(1<<slot);
	mfc_read_tag_status_all();
}

void large_get_blocking(ea_addr_t from, ls_addr_t to, ls_size_t size, int slot){
	large_get(from, to, size, slot);
	mfc_write_tag_mask(1<<slot);
	mfc_read_tag_status_all();
}

